<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class infocate extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("minfo");
        $this->load->model("minfocate");
        $this->load->model("mresource");
		
		$this->resCode = $this->minfocate->resCode;
        $this->url_index = admin_url() . "infocate";
        $this->folder_upload = "dataupload/infocate/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $data['datas'] = $this->minfocate->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = "infocate/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($infoCateID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['infocate'] = $this->minfocate->get($infoCateID, 'INFCATE_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
		$arr_condition =array();
		if($infoCateID >0)
			$this->db->where("INFCATE_ID !=",$infoCateID);
		$cateParents = $this->minfocate->gets(array('INFCATE_STATUS'=>1,'INFCATE_PARENT'=>0));
		$data['cateParent'] = array();
		$data['cateParent'][0] = '-----';
		if($cateParents)
		foreach($cateParents as $r){
			$data['cateParent'][$r->INFCATE_ID] = $r->INFCATE_TITLE;
		}
		
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
            if ($infoCateID > 0) {
                $this->minfocate->update($infoCateID, 'INFCATE_ID', $post['infocate']);
            }
			else{
				$infoCateID = $this->minfocate->insert($post['infocate']);
			}
			$this->buid_category_router();
            redirect($this->url_edit . $infoCateID);
        }
        $this->theme_data["main"] = "infocate/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($infoCateID = 0){
		if($infoCateID > 0){
			$this->minfo->delete($infoCateID, 'INFCATE_ID');
			$this->minfocate->delete($infoCateID, 'INFCATE_ID');
		}
	}
	
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
	}
	function buid_category_router(){
	$this->load->helper('file');
		$cates = $this->minfocate->gets();
		$data_temp =array();
		$data = "<?php \n";
        $data.="return ";
		if($cates) {
			foreach($cates as $cate){
			if($cate->INFCATE_CODE != '')
				$data_temp[] = strtolower($cate->INFCATE_CODE);
			}
		$data_temp = var_export($data_temp, true);
		$data.= $data_temp;
        $data.="?>\n";
        write_file('./mcache/category_router.php', $data);
		}
	}
}
